//
//  DestinationViewController.m
//  LT
//
//  Created by dllo on 16/5/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DestinationViewController.h"
#import "DesHot_countryModel.h"
#import "DestinationTableViewCell.h"
#import "DescountryModel.h"
#import "detinationSecondTableViewCell.h"
#import "ZHNetworking.h"
#import "DestinationTableViewCell.h"

static NSString *const HotCountryCell = @"hotCountryCell";
static NSString *const countryCell = @"CountryCell";

@interface DestinationViewController ()<UITableViewDataSource,UITableViewDelegate>
//建立tableView
@property(nonatomic,strong)UITableView *DestiTableView;

//缓存需要的数组
@property(nonatomic,strong)NSMutableArray *readArray;


@property(nonatomic,strong)UIImageView *mapImage;

//需要用的各种数组
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *hotCountryArray;
@property(nonatomic,strong)NSMutableArray *countryArray;

//再建一个tempArray用在button的case语句中
@property(nonatomic,strong)NSMutableArray *tempArray;


@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //目的地页背景颜色
     self.view.backgroundColor = [UIColor redColor];
    self.dataArray = [NSMutableArray array];
    [self read];
    [self getData];
    self.DestiTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    [self.view addSubview:self.DestiTableView];
    self.DestiTableView.delegate = self;
    self.DestiTableView.dataSource = self;
    self.DestiTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
 
    self.navigationController.navigationBarHidden = NO;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"btn_actionsheet"] forBarMetrics:UIBarMetricsDefault];
    self.navigationItem.hidesBackButton = YES;
    
    //增加头视图(直接把地图设置成头视图)
    self.mapImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 267 * WIDTHCELL)];
    [self.mapImage setImage:[UIImage imageNamed:@"map"]];
    self.DestiTableView.tableHeaderView = self.mapImage;
    self.mapImage.userInteractionEnabled = YES;
    //注册
    [self.DestiTableView registerClass:[DestinationTableViewCell class] forCellReuseIdentifier:HotCountryCell];
    [self.DestiTableView registerClass:[detinationSecondTableViewCell class] forCellReuseIdentifier:countryCell];
    //======7个按钮添加======
    UIButton *buttonAsia = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonAsia.frame = CGRectMake(300 * WIDTHCELL, 70 * WIDTHCELL, 60 * WIDTHCELL, 30 * WIDTHCELL);
    buttonAsia.backgroundColor = [UIColor clearColor];
    [buttonAsia setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonAsia setTitle:@"亚洲" forState:UIControlStateNormal];
    [buttonAsia setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonAsia];
    [buttonAsia addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonAsia.tag = 0;
    
    UIButton *buttonEurope = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonEurope.frame = CGRectMake(200 * WIDTHCELL, 50 * WIDTHCELL, 60 * WIDTHCELL, 30 * WIDTHCELL);
    buttonEurope.backgroundColor = [UIColor clearColor];
    [buttonEurope setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonEurope setTitle:@"欧洲" forState:UIControlStateNormal];
    [buttonEurope setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonEurope];
   [buttonEurope addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
     buttonEurope.tag = 1;
    
        UIButton *buttonNorAmerica = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonNorAmerica.frame = CGRectMake(60 * WIDTHCELL, 40 * WIDTHCELL, 70 * WIDTHCELL, 30 * WIDTHCELL);
    buttonNorAmerica.backgroundColor = [UIColor clearColor];
    [buttonNorAmerica setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonNorAmerica setTitle:@"北美洲" forState:UIControlStateNormal];
    [buttonNorAmerica setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonNorAmerica];
    [buttonNorAmerica addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonNorAmerica.tag = 2;
    
        UIButton *buttonSouAmerica = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonSouAmerica.frame = CGRectMake(100 * WIDTHCELL, 120 * WIDTHCELL, 70 * WIDTHCELL, 30 * WIDTHCELL);
    buttonSouAmerica.backgroundColor = [UIColor clearColor];
    [buttonSouAmerica setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonSouAmerica setTitle:@"南美洲" forState:UIControlStateNormal];
    [buttonSouAmerica setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonSouAmerica];
    [buttonSouAmerica addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonSouAmerica.tag = 3;
   
    
    UIButton *buttonOceania = [UIButton buttonWithType:UIButtonTypeCustom];
   buttonOceania.frame = CGRectMake(320 * WIDTHCELL, 140 * WIDTHCELL, 70 * WIDTHCELL, 30 * WIDTHCELL);
    buttonOceania.backgroundColor = [UIColor clearColor];
    [buttonOceania setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonOceania setTitle:@"大洋洲" forState:UIControlStateNormal];
    [buttonOceania setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonOceania];
    [buttonOceania addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonOceania.tag = 4;
    
    UIButton *buttonAfrica = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonAfrica.frame = CGRectMake(200* WIDTHCELL, 100* WIDTHCELL, 60* WIDTHCELL, 30* WIDTHCELL);
    buttonAfrica.backgroundColor = [UIColor clearColor];
    [buttonAfrica setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonAfrica setTitle:@"非洲" forState:UIControlStateNormal];
    [buttonAfrica setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonAfrica];
    [buttonAfrica addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonAfrica.tag = 5;
    
    UIButton *buttonAntarctica = [UIButton buttonWithType:UIButtonTypeCustom];
   buttonAntarctica.frame = CGRectMake(200* WIDTHCELL, 220* WIDTHCELL, 70* WIDTHCELL, 30* WIDTHCELL);
   buttonAntarctica.backgroundColor = [UIColor clearColor];
    [buttonAntarctica setBackgroundImage:[UIImage imageNamed:@"chart"] forState:UIControlStateNormal];
    [buttonAntarctica setTitle:@"南极洲" forState:UIControlStateNormal];
    [buttonAntarctica setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
    [self.mapImage addSubview:buttonAntarctica];
    [buttonAntarctica addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    buttonAntarctica.tag = 6;
    
}


//========七个button对应一个点击方法
-(void)buttonAction:(UIButton *)button{
    DestinationModel *Asia = [self.dataArray objectAtIndex:0];
    DestinationModel *Europe = [self.dataArray objectAtIndex:1];
    DestinationModel *NorAmerica = [self.dataArray objectAtIndex:2];
    DestinationModel *SouAmerica = [self.dataArray objectAtIndex:3];
    DestinationModel *Oceania = [self.dataArray objectAtIndex:4];
    DestinationModel *Africa = [self.dataArray objectAtIndex:5];
    DestinationModel *Antarctica = [self.dataArray objectAtIndex:6];
    NSLog(@"%@",Europe.hot_country);
    switch (button.tag) {
        case 0:
            self.hotCountryArray =[NSMutableArray arrayWithArray:Asia.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:Asia.country];
            [self.DestiTableView reloadData];
            
            break;
           
        case 1:
            self.hotCountryArray = [NSMutableArray arrayWithArray:Europe.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:Europe.country];
            [self.DestiTableView reloadData];
          
        break;
            
            
        case 2:
            self.hotCountryArray = [NSMutableArray arrayWithArray:NorAmerica.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:NorAmerica.country];
            [self.DestiTableView reloadData];
            
            
            break;
            
            
        case 3:
            self.hotCountryArray = [NSMutableArray arrayWithArray:SouAmerica.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:SouAmerica.country];
            [self.DestiTableView reloadData];
            
            break;
            
            
        case 4:
            self.hotCountryArray = [NSMutableArray arrayWithArray:Oceania.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:Oceania.country];
            [self.DestiTableView reloadData];

            break;
            
            
        case 5:
            self.hotCountryArray = [NSMutableArray arrayWithArray:Africa.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:Africa.country];
            [self.DestiTableView reloadData];

            break;
            
        case 6:
            self.hotCountryArray = [NSMutableArray arrayWithArray:Antarctica.hot_country];
            self.tempArray = [NSMutableArray arrayWithArray:Antarctica.country];
            [self.DestiTableView reloadData];
 
            break;
            
            
            
        default:
            break;
    }
    
}


//读取缓存数据
-(void)read{
    self.readArray = [NSMutableArray array];
    //取数据从本地plist
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSString *destinationPath = [path stringByAppendingPathComponent:@"destination.plist"];
    NSArray *localDestinationArray = [NSArray arrayWithContentsOfFile:destinationPath];
    if (localDestinationArray.count != 0) {
        
        for (NSDictionary *countryDic in localDestinationArray) {
            DestinationModel *desModel = [[DestinationModel alloc]init];
            [desModel setValuesForKeysWithDictionary:countryDic];
            [self.dataArray addObject:desModel];
            
        }
  
    }
    

}


//网络数据请求
-(void)getData{
[ZHNetworking zhGETWithURL:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88250864194671&lon=121.5393777429208&oauth_token=51c16ac42c049d7967cec9579b89a89d&page=1&track_app_channel=App%2520Store&track_app_version=6.8.5&track_device_info=iPhone%25205s&track_deviceid=CBF73BD2-C65E-4BC1-9574-89BDE1EE9C36&track_os=ios%25209.2.1&track_user_id=7664574&v=1"  parameter:nil succes:^(id result) {
    
    //result是最外面的大字典
    NSDictionary *dic = result;
    NSMutableArray *dataArray = [dic objectForKey:@"data"];
    
    
    //写入缓存
    //找到Document路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",path);//打印的目的是为了确定这个路径对不对
    //拼接一个自己的路径
    NSString *deatinationPath = [path stringByAppendingPathComponent:@"destination.plist"];
    //写入数据到对应路径的plist文件中
    [dataArray writeToFile:deatinationPath atomically:YES];
    
    
    for (NSDictionary *countryDic in dataArray) {
        DestinationModel *desModel = [[DestinationModel alloc]init];
        [desModel setValuesForKeysWithDictionary:countryDic];
        [self.dataArray addObject:desModel];
       
    }
    //默认亚洲
    DestinationModel *Asia = [self.dataArray objectAtIndex:0];
    self.hotCountryArray = [NSMutableArray arrayWithArray:Asia.hot_country];
    self.countryArray = [NSMutableArray arrayWithArray:Asia.country];
    [self.DestiTableView reloadData];
    
    
} failure:^(NSError *error) {
    
    
}];


}


//=============================
#pragma mark 设置分区行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
      
        return 1120* WIDTHCELL;
    }
    return 60* WIDTHCELL;
    
}

#pragma mark 设置section数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
    
}

#pragma mark 设置section标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        //根据数组更改
        return @"热门旅游胜地";
        
    }
    return @"其余旅游地点";
    
    
}

#pragma mark tableView每个分区要显示的行数(cell的个数)必须实现的方法之一
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //return 50;//(50是行数)
    if (section == 0) {
        return 1;//为什么是1
    }
    
    return self.countryArray.count;
}

#pragma mark 必须实现的方法之2,作用是每显示一个cell都会执行这个方法.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        DestinationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HotCountryCell];
        //吧自身的array赋值给cell
        cell.destinationArray = self.hotCountryArray;
        return cell;
    }
    

    detinationSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:countryCell];
    DescountryModel *country = [self.countryArray objectAtIndex:indexPath.row];
    cell.countryModel = country;
    return cell;

    
}

////加一个cell的点击方法
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    //建立通知中心对象,给通知一个名字和一个点击方法
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changHUD:) name:@"tongzhi" object:nil];
//
//
//
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
